#include "box.h"

#include "shapes3d.h"
#include <gl\glew.h>
#include <gl\glfw.h>

#include "ball.h"

using namespace TorPhysix;

void Box::vRenderShadow()
{
	glPushMatrix();
	{
		glTranslatef(0.0f, 0.03f, 0.0f);
		glScalef(1.0f, 0, 1.0f);
		float tform[16];
		collisionPrimitive->body->getGLTransform(tform);
		glMultMatrixf(tform);
		glScalef(collisionBox->halfSize.x()*2, collisionBox->halfSize.y()*2, collisionBox->halfSize.z()*2);
		Shapes3D::DrawCube(1.0f);
	}
	glPopMatrix();
}

void Box::vRender()
{
	glPushMatrix();
	float tform[16];
	collisionBox->body->getGLTransform(tform);
	glMultMatrixf(tform);
	glScalef(collisionBox->halfSize.x()*2, collisionBox->halfSize.y()*2, collisionBox->halfSize.z()*2);

	Shapes3D::DrawCube(1.0f);
	glPopMatrix();
}

void Box::vPicking()
{
	glPushMatrix();
	float tform[16];
	collisionBox->body->getGLTransform(tform);
	glMultMatrixf(tform);
	glScalef(collisionBox->halfSize.x()*2, collisionBox->halfSize.y()*2, collisionBox->halfSize.z()*2);

	glColor3f(m_colorID[0]/255.0f, m_colorID[1]/255.0f, m_colorID[2]/255.0f);
	Shapes3D::DrawCube(1.0f);
	glPopMatrix();
}

void TorPhysix::Box::vScale( Vec3f scaleAxis, float scaleMagnitude )
{
	Vec3f scaleDelta = scaleAxis * scaleMagnitude;
	collisionBox->scale(scaleDelta);
}

void TorPhysix::Box::vRenderDebug()
{
	glColor3f(1,0,1);
	glBegin(GL_LINES);
	const Vec3f &pos = collisionBox->body->getPosition();
	glVertex3f( pos.x(), pos.y(), pos.z() );

	const Vec3f &rot = collisionBox->body->getRotation();
	const Vec3f rotPos = pos + rot;
	glVertex3f( rotPos.x(), rotPos.y(), rotPos.z() );
	glEnd();
}

void TorPhysix::Box::collideAgainst( Ball& ball, CollisionData* cData )
{
	ball.collideAgainst( *this, cData);
}

void TorPhysix::Box::collideAgainst( Box& box, CollisionData* cData )
{
	if( this->collisionPrimitive->body->isStatic && box.collisionPrimitive->body->isStatic) return;

	if( cData->hasMoreContacts() )
	{
		CollisionDetector::boxAndBox( *(this->collisionBox), *(box.collisionBox), cData );
	}
}